package com.darrenchan.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-29 22:07
 */
public class Q349 {
    public int[] intersection(int[] nums1, int[] nums2) {
        if(nums1 == null || nums2 == null) {
            return null;
        }
        Set<Integer> set1 = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set1.add(nums1[i]);
        }

        Set<Integer> set2 = new HashSet<>();
        for (int i = 0; i < nums2.length; i++) {
            if(set1.contains(nums2[i])){
                set2.add(nums2[i]);
            }
        }

        int[] res = new int[set2.size()];
        int index = 0;
        for (Integer o : set2) {
            res[index++] = o;
        }
        return res;
    }
}
